package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.order.biz.TransOutboundInvoker;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.factory.OrderProcessPipelineFactory;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.dto.OrderCancelDto;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.service.OrderCoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderCoreServiceImpl implements OrderCoreService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderProcessPipelineFactory orderProcessPipelineFactory;

    @Autowired
    StockMapper stockMapper;


    /**
     * 创建订单的处理流程
     *
     * @param request
     * @return
     */
    @Override
    public String createOrder(CreateOrderRequest request) {
        String orderId = null;
        try {
            //创建pipeline对象
            TransOutboundInvoker invoker = orderProcessPipelineFactory.build(request);

            //启动pipeline
            invoker.start(); //启动流程（pipeline来处理）

            //获取处理结果
            AbsTransHandlerContext context = invoker.getContext();

            //从Context中获取订单Id
            orderId = context.getOrderId();

        } catch (Exception e) {
            log.error("OrderCoreServiceImpl.createOrder Occur Exception: "+ e);
            throw e;

        }
        return orderId;
    }

    @Override
    public void cancelOrder(String orderId) {

        updateOrder(OrderConstants.ORDER_STATUS_TRANSACTION_CANCEL,orderId);

        List<OrderCancelDto> item = orderItemMapper.getItemsByOrderId(orderId);


        orderItemMapper.updateStockStatus(OrderConstants.ORDER_ITEM_STATUS_RELEASE,orderId);

        for (OrderCancelDto orderCancelDto : item) {
            stockMapper.UpdateStockByCancelOrder(orderCancelDto.getItem_Id(),orderCancelDto.getNum());
        }



    }

    @Override
    public void deleteOrder(String orderId) {
        try {
            orderMapper.deleteOrderByOrderId(orderId);
            orderItemMapper.deleteOrderItemByOrderId(orderId);
            orderShippingMapper.deleteOrderShippingByOrderId(orderId);
        } catch (Exception e) {
            log.error("OrderCoreServiceImpl.deleteOrder Occur Exception:" + e);
            e.printStackTrace();
        }

    }

    @Override
    public void updateOrder(Integer status, String orderId) {
        orderMapper.cancelOrderByOrderId(status,orderId);
    }

}
